<template>
	<view>
		<!-- header部分 -->
		<view class='header-Container header-height'>
			<view class="header-bg"></view>
			<navHeader pageTitle="贡献" colorTheme="white" hideBackBtn>
				<template v-slot:rightBtns>
					<view class="row-div" style="position: relative;margin-right: 40upx;" @click="gotoMsgCenter">
						<image src="/static/images/index/message.png" style="width: 50upx;height: 50upx;" />
						<view v-if="noticeTipCount || $store.state.msgNumber" class="msgCount">{{noticeTipCount + $store.state.msgNumber>99 ? '99+' : noticeTipCount + $store.state.msgNumber}}</view>
					</view>
				</template>
			</navHeader>
		</view>
		<!-- <load-refresh ref="loadRefresh" isRefresh refreshType="hollowDots" :pageNo="1" :totalPageNo="1" color="#FE5E49"
		 heightReduce="0" backgroundCover="#F3F5F5" @refresh="onRefresh"> -->
		<view class="column-start content">
			<view class="column-start card-view">
				<view class="row-between" style="width: 100%;margin-bottom: 42upx;">
					<view style="flex: 1;color: #FFFFFF;">
						<view style="font-size: 24upx;margin-bottom: 12upx;">今日贡献值</view>
						<view style="font-size: 68upx;font-weight: bold;">{{moneyBalance}}</view>
					</view>
					<view style="width: 40upx;"></view>
					<view style="flex: 1;color: #FFFFFF;">
						<view class="row-start" style="margin-bottom: 12upx; position: relative;">
							<view style="font-size: 24upx;margin-right: 11upx;">待结算贡献值</view>
							<image src="/static/assets/question.png" style="width: 22upx;height: 22upx;" @click="remarkStatus = true"></image>
						</view>
						<view style="font-size: 68upx;font-weight: bold;">{{consumeBalance}}</view>
					</view>
				</view>
				<view class="row-between" style="width: 100%;color: #FFFFFF;">
					<view style="font-size: 20upx;">每日可结算贡献值24点计算权益分并清零</view>
					<navigator url="/pages/detail/contributionDetails/contributionDetails" class="row-div contribution-view">
						<view>贡献值明细</view>
						<uni-icons type="arrowright" style="font-size: 20upx;" color="#FFFFFF"></uni-icons>
					</navigator>
				</view>
			</view>

			<!-- 幸运转盘 -->
			<navigator :url="'/pagesAccount/signIn/luckTow'" hover-class="none">
				<image src="/static/assets/banner.png" style="width: 690upx;height: 176upx;margin-bottom: 53upx;" />
			</navigator>

			<view style="margin-bottom: 35upx;"><text class="everyday_contribute">每日贡献</text></view>
			<view v-for="item in contributionList" :key="item.id + item.title" class="row-between" style="margin-bottom: 46upx; width: 100%;">
				<view class="row-start">
					<image :src="imgSite+item.cover" style="width: 80upx;height: 80upx;margin-right: 21upx;" />
					<view>
						<view style="font-size: 26upx;color: #333333;font-weight: bold;margin-bottom: 14upx;">{{item.title}}</view>
						<view class="ruleDepict-css" v-html="item.ruleDepict"></view>
						<!-- <view style="font-size: 24upx;color: #999999;">每次可得 {{item.contribution}} 贡献值 {{item.type != 50 && item.type != 40 ? `完成${item.completeNumber}/${item.limitTimes}` : ''}}</view> -->
					</view>
				</view>
				<view class="button_contribute" @click="makeContribution(item)">做贡献</view>
			</view>
		</view>
		<!-- </load-refresh> -->
		
		<view class="column-div arrowright-css" v-if="remarkStatus" @click="remarkStatus = false">
			<view class="column-div arrowright-text" @click.stop>
				<uni-icons type="closeempty" class="closeempty-css" @click="remarkStatus = false"></uni-icons>
				<view style="font-size: 28upx;font-weight: bold;color: #333333;margin-bottom: 35upx;">待结算贡献值</view>
				<view class="arrowright-text-css">{{freezeBalanceRemark}}</view>
			</view>
			
		</view>
		<popupDialogComponent ref='popupDialog' :content="'您还未进行实名认证，需实名认证后才可以获得贡献值，是否立即认证？'" leftBtnText="暂不认证" rightBtnText="立即认证"
		 @close="close" @confirm="confirm">
		</popupDialogComponent>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	import navHeader from '@/components/nav-header/nav-header';
	// #ifdef APP-PLUS
	const JDopen = uni.requireNativePlugin('UZK-JDopen');
	import fusionSdk from '../../common/fusionAd.js'
	// #endif
	export default {
		components: {
			navHeader
		},
		computed: {
			...mapState(['hasLogin', 'downUrl', 'imgSite', 'url'])
		},
		data() {
			return {
				remarkStatus: false,
				triggered: false,
				_triggered: false,
				freezeBalanceRemark: '',
				/* 今日贡献值 */
				moneyBalance: 0,
				consumeBalance: 0,
				/* 每日贡献列表 */
				contributionList: [],
				/* 用户信息 */
				userInfo: null,
				noticeTipCount: 0,
				openId: null
			}
		},
		onLoad() {
			// #ifdef APP-PLUS
			JDopen.init(result => console.log("init", JSON.stringify(result)))
			// #endif
			
		},
		onReady () {
			
			
		},
		onShow() {
			console.log("onShow-------------getSystemInfo")
			uni.getSystemInfo({
				success: (res) => {
					console.log("success", JSON.stringify(res))
					console.log("success", JSON.stringify(res.safeArea))
				},
				fail: (res) => {
					console.log("fail", res)
				},
				complete: () => {
					console.log("complete")
				}
			})
			this.infoRequest()
			this.getUserInfo()
			this.getBalance();
			this.$api.globalUtil.setTabBarBadge();
		},
		onHide: function() {
			if (!this.openId) return;
			this.getCloseAdvertising(this.openId, 40)
			console.log("onHide----function------------------------------------");
		},
		methods: {
			onRefresh() {
				this.contributionList = []
				this.infoRequest();
			},
			// 做贡献
			makeContribution(item) {
				// 10 看视频 20 看广告 30 看新闻 40 优惠购 50 邀请好友
				// #ifdef APP-PLUS
				console.log("做任务", item, item.type)
				this.openId = item.id
				if (item.type == 10) return this.openVideo(item.id); // 看视频
				if (item.type == 20) return this.openAdvertising(item.id); // 看广告
				if (item.type == 30) return this.openNews(item.id); // 看新闻 ---- 开屏广告
				if (item.type == 40) return this.goJDopen(); // 优惠购
				if (item.type == 50) { // 邀请好友
					uni.navigateTo({
						url: '/pagesAccount/extension/extension'
					})
				}
				// #endif

				// this.$api.globalUtil.requestAjax({
				// 	url: '/makeContributionApi/contribution',
				// 	method: "POST",
				// 	data: {
				// 		contributionId: item.id
				// 	},
				// 	success: (res) => {
				// 		if (res.data.success) {

				// 			res.data.response ? this.findContributionList() : this.$api.msg('已完成');
				// 		}
				// 	},
				// });
			},
			openAdvertising(id) {
				let platform = uni.getSystemInfoSync().platform,
					state = this.$store.state
				let codeId = platform == 'android' ? state.android_advertising_code : state.ios_advertising_code;
				this.$api.showLoading()
				let _this = this
				fusionSdk.loadInterstitialAd(codeId, {
					onAdShow: function() {
						uni.hideLoading()
						_this.getOpenAdvertising(id, 40)
						console.log("onInterShow")
					},
					onAdClick: function() {
						console.log("onInterClick")
					},
					onAdClosed: function() {
						console.log("onInterClosed")
						uni.hideLoading()
						_this.getCloseAdvertising(id, 40)
						_this.infoRequest()
					},
					onAdError: function(code, msg) {
						uni.showToast({
							title: msg
						})
					}
				})
			},

			openVideo(id) {
				let platform = uni.getSystemInfoSync().platform,
					state = this.$store.state
				let codeId = platform == 'android' ? state.android_video_code : state.ios_video_code;
				let entity = JSON.stringify({
					contributeId: id
				})
				this.$api.showLoading()
				let _this = this
				fusionSdk.loadRewardVideoAd({
					"codeId": codeId,
					"userId": this.userInfo.id,
					"ext": entity,
				}, {
					onAdShow: () => {
						uni.hideLoading()
					},
					onAdClick: function() {
						console.log("onRewardVideoClick")
					},
					onAdClosed: function() {
						uni.hideLoading()
						_this.infoRequest()
					},
					onAdVideoComplete: function() {
						uni.hideLoading()
					},
					onAdReward: function() {
						console.log("onAdReward")
						uni.hideLoading()
					},
					onAdError: function(code, msg) {
						console.log("onAdError", code, msg)
						uni.showToast({
							title: msg,
							icon: 'none',
							duration: 2000,
							mask: true
						})
					}
				});
			},

			openNews(id) {
				return uni.navigateTo({
					url: '/pages/splash/splash?id=' + id
				})
			},
			goJDopen() {
				this.$api.showLoading()
				this.$api.globalUtil.requestAjax({
					url: '/jdUnion/promotionCommonGet',
					method: "POST",
					data: {
						terminal: uni.getSystemInfoSync().platform == 'android' ? 10 : 20
					},
				}).then(res => {
					if (res.data.code != 200) return uni.hideLoading();
					JDopen.openurl({
						url: res.data.data.clickURL,
					}, (res) => {
						console.log("JDopen", JSON.stringify(res));
						uni.hideLoading()
						if (!res.false) return this.$api.msg(res.msg);
						this.infoRequest()
					});
				})
			},
			async infoRequest() {
				this.openId = null
				console.log("infoRequest--------------------")
				this.$api.showLoading();
				await this.getMsgCount();
				await this.findContributionList();
				uni.hideLoading()
				// setTimeout(() => this.$refs.loadRefresh.completed(), 500)
			},
			// 获取每日贡献值列表
			async findContributionList() {
				return await this.$api.globalUtil.requestAjax({
					url: '/contributionApi/findContributionList',
					method: "POST",
					data: {},
					success: (res) => {
						if (res.data.success) {
							this.contributionList = res.data.response
							this.todayContribution = 0
							this.contributionList.forEach(item => this.todayContribution += item.contribution * item.completeNumber)
						}
					},
					/* complate: () =>  uni.stopPullDownRefresh() */
				});
			},
			// 看广告
			getOpenAdvertising(id, type) {
				console.debug("contributionId:", id)
				return this.$api.globalUtil.requestAjax({
					url: '/makeContributionApi/watchAds',
					method: "POST",
					data: {
						contributionId: id,
						rewardType: type || ''
					},
					success: (res) => {
						console.debug(res)
						this.infoRequest()
					}
				})
			},
			// 关闭广告
			getCloseAdvertising(id, type) {
				return this.$api.globalUtil.requestAjax({
					url: '/makeContributionApi/closeAd',
					method: "POST",
					data: {
						contributionId: id,
						rewardType: type || ''
					},
					success: (res) => {
						console.debug(res)
						this.infoRequest()
					}
				})
			},

			getMsgCount() {
				return this.$api.globalUtil.requestAjax({
					url: '/customerNoticeApi/customerNoticeUnreadCountApi',
					method: 'POST',
					data: {
						cnType: "",
						cnStatus: '0'
					},
					success: (res) => this.noticeTipCount = res.data.response
				});
			},
			// goto消息中心
			gotoMsgCenter() {
				let that = this;
				that.$api.globalUtil.methodFilter({
					that: that,
					success: function() {
						uni.navigateTo({
							url: '/pagesUser/msgCenter'
						});
					}
				});
			},
			getBalance() {
				return this.$api.globalUtil.requestAjax({
					url: '/AccountBalanceApi/findAccountBalanceByCustomerIdForApi',
					method: 'POST',
					data: {},
					success: (res) => {
						this.moneyBalance = res.data.response.moneyBalance
						this.consumeBalance = res.data.response.consumeBalance
						this.freezeBalanceRemark = res.data.response.freezeBalanceRemark
					}
				});
			},
			// 获取用户信息
			getUserInfo() {
				return this.$api.globalUtil.getUserInfo(this, (res) => {
					res.data.response.confirmation == 10 ? this.$refs.popupDialog.openPopup() : null;
					this.userInfo = res.data.response
				});
			},
			close(done) {
				done()
			},
			confirm(done, value) {
				let data = JSON.stringify(this.userInfo);
				uni.navigateTo({
					url: '/pagesUser/realName?userInfo=' + data
				});
				done()
			},
		}
	}
</script>

<style scoped lang="scss">
	.arrowright-css {
		width: 100%;
		height: 100vh;
		position: fixed;
		z-index: 98999;
		top: 0;
		left: 0;
		background: rgba(0,0,0,0.7);
	}

	.arrowright-text {
		width: 540upx;
		padding: 30upx;
		background: #FFFFFF;
		border-radius: 16upx;
		position: relative;
		.closeempty-css{
			position: absolute;
			top: 10upx;
			right: 10upx;
			color: #999999;
		}
		.arrowright-text-css{
			font-size: 24upx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #333333;
		}
	}

	.refresh-scroll {
		background: #FFFFFF;
		width: 100%;
		height: calc(100vh - 95rpx - 50px);

		/* #ifdef APP-PLUS */
		height: calc(100vh - (var(--status-bar-height) + 95rpx));
		/* #endif */

		/* #ifdef MP-WEIXIN */
		height: calc(100vh - (var(--status-bar-height) + 178upx));
		/* #endif */
	}

	.loadNews-view {
		height: 1300upx;
		width: 100%;
	}

	.ruleDepict-css {
		font-size: 24upx;
		color: #999999;
	}

	/* header部分 */
	.header-Container {
		width: 100%;
		height: 176rpx;
		overflow: hidden;
		//box-shadow: 0px 1rpx 1rpx 0px rgba(0, 0, 0, 0.08);
		/* background: rgba(255, 255, 255, 1); */

		background: #FE5E49;

		.leftBtns {
			display: flex;
			align-items: center;
			justify-content: flex-start;
			position: absolute;
			bottom: 0;
			width: 100%;
			height: 86rpx;

			.back {
				margin-left: 17rpx;
				width: 44rpx;
				height: 44rpx;
			}
		}

		/deep/ .title {
			position: absolute;
			bottom: 0;
			width: 100%;
			height: 88rpx;
			text-align: center;
			font-size: 36rpx;
			font-family: PingFang SC;
			font-weight: bold;
			color: #FFFFFF;
		}

		.msgCount {
			position: absolute;
			top: -9rpx;
			right: -9.5rpx;
			box-shadow: 0px 1rpx 4rpx 0px rgba(68, 68, 68, 0.2);
			min-width: 26rpx;
			width: auto;
			height: 26rpx;
			line-height: 26rpx;
			text-align: center;
			font-size: 20rpx;
			font-family: PingFang SC;
			font-weight: bold;
			color: #FE5E49;
			background: rgba(255, 255, 255, 1);
			box-shadow: 0px 1rpx 4rpx 0px rgba(68, 68, 68, 0.2);
			border-radius: 13rpx;
		}
	}

	.card-view {
		width: 100%;
		margin: auto;
		background: linear-gradient(-30deg, #FF9300 0%, #FF7800 100%);
		border-radius: 16upx;
		margin: auto;
		padding: 0 30upx;
		padding-top: 40upx;
		padding-bottom: 30upx;
		align-items: flex-start;
		position: relative;
		margin-bottom: 20upx;

		&::after {
			content: " ";
			background-image: url(../../static/assets/pic.png);
			background-size: 100%;
			width: 92upx;
			height: 92upx;
			position: absolute;
			bottom: 79upx;
			right: 48upx;
		}

		.contribution-view {
			position: relative;
			padding: 8upx 15upx;
			border: 1px solid #FFFFFF;
			box-shadow: 4upx 4upx 10upx 0px rgba(120, 120, 120, 0.1);
			border-radius: 30upx;
			font-size: 20upx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #FFFFFF;
		}

	}

	.my-badge {
		background-color: #FFFFFF;
		border: 1px solid #FFFFFF;
		color: #FE5E49;
		position: absolute;
		right: -20upx;
		top: -15upx;
	}

	.content {
		width: 100%;
		border: 1upx solid ridge;
		background-color: #FFFFFF;
		padding: 30upx;
	}

	.contribute {
		width: 690upx;
		height: 260upx;
		background: linear-gradient(-30deg, #FF9300 0%, #FF7800 100%);
		border-radius: 16upx;
		margin-bottom: 30upx;
		padding: 46upx;
		padding: 46upx 36upx 0upx 40upx;
	}

	.everyday_contribute {
		width: 119upx;
		height: 29upx;
		font-size: 30upx;
		font-family: PingFang SC;
		font-weight: bold;
		color: #333333;
		line-height: 22upx;
	}

	.button_contribute {
		padding: 10upx 30upx;
		background: #FE5E49;
		border-radius: 27upx;
		font-size: 24upx;
		color: #FFFFFF;
	}

	.row-between-a {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.today_contribute {
		width: 100%;
		margin-bottom: 24upx;
		font-size: 24upx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #FFFFFF;
	}

	.today_num {
		font-size: 68upx;
		font-family: PingFang SC;
		font-weight: bold;
		color: #FFFFFF;
	}

	.today_text {
		font-size: 24upx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #FEEBD0;
	}

	.contribute_detail {
		width: 180upx;
		height: 44upx;
		border: 1upx solid #FFFFFF;
		border-radius: 22upx;
		font-size: 12upx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #FFFFFF;
		line-height: 39upx;
		text-align: center;
	}
</style>
